package com.easyspringboot.bulider;

import com.easyspringboot.bean.Constants;
import com.easyspringboot.bean.FieldInfo;
import com.easyspringboot.bean.TableInfo;
import com.easyspringboot.utils.DateUtils;
import com.easyspringboot.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * @Author xiaoyi
 * @Version 1.0
 * @ClassName BuildPo
 * @Time 21:33
 */
public class BuildPo {
    private static final Logger logger = LoggerFactory.getLogger(BuildPo.class);

    public static void execute(TableInfo tableInfo) {
        File folder = new File(Constants.PATH_PO);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        File poFile = new File(folder, tableInfo.getBeanName() + ".java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(poFile);
            outw = new OutputStreamWriter(out, "utf8");
            bw = new BufferedWriter(outw);
            bw.write("package " + Constants.PACKAGE_PO + ";");
            bw.newLine();
            bw.newLine();
            bw.write("import java.io.Serializable;");
            bw.newLine();
//            logger.info("Date:{},DateTime:{}",tableInfo.getHavaDate(),tableInfo.getHaveDateTime());
            //判断是否有日期类型

            if (tableInfo.getHavaDate()||tableInfo.getHaveDateTime()) {
                bw.write("import "+Constants.PACKAGE_ENUM+".DateTimeEnum;");
                bw.newLine();
                bw.write("import "+Constants.PACKAGE_UTILS+".DateUtils;");
                bw.newLine();
                bw.newLine();
                bw.write("import java.util.Date;");
                bw.newLine();
                bw.newLine();
                bw.write(Constants.BEAN_DATE_FORMAT_CLASS + ";");
                bw.newLine();
                bw.write(Constants.BEAN_DATE_UNFORMAT_CLASS + ";");
                bw.newLine();


            }

            bw.newLine();
            //忽略字段
            Boolean haveIgnoreBean = false;
            for (FieldInfo fied : tableInfo.getFieldList()) {
                if (ArrayUtils.contains(Constants.IGNORE_BEAN_TOJSON_FIELD.split(","), fied.getPropertyName())) {
                    haveIgnoreBean = true;
                    break;
                }
            }
            if (haveIgnoreBean) {
                bw.write(Constants.IGNORE_BEAN_TOJSON_CLASS + ";");
                bw.newLine();
            }

            bw.newLine();
            if (tableInfo.getHaveBigDecimal()) {
                bw.write("import java.math.BigDecimal;");
                bw.newLine();
            }
            bw.newLine();
            bw.newLine();
            //创建类注释
            BuildComment.createClassComment(bw, tableInfo.getComment());
            bw.write("public class " + tableInfo.getBeanName() + " implements Serializable{");
            bw.newLine();
            bw.newLine();
            for (FieldInfo fied : tableInfo.getFieldList()) {
                BuildComment.createFieldComment(bw, fied.getComment());//创建属性注释

                //判断是否需要忽略
                if (ArrayUtils.contains(Constants.IGNORE_BEAN_TOJSON_FIELD.split(","), fied.getPropertyName())) {
                    bw.write("\t"+String.format(Constants.IGNORE_BEAN_TOJSON_EXPRESSION, DateUtils.YYYYMMDD));
                    bw.newLine();
                }
                //判断是否有日期类型
                if (ArrayUtils.contains(Constants.SQL_DATA_TIME_TYPES, fied.getSqlType())) {
                    bw.write("\t" + String.format(Constants.BEAN_DATE_FORMAT_EXPRESSION, DateUtils.YYYY_MM_DD_HH_MM_SS));
                    bw.newLine();
                    //反序列化
                    bw.write("\t" + String.format(Constants.BEAN_DATE_UNFORMAT_EXPRESSION, DateUtils.YYYY_MM_DD_HH_MM_SS));
                    bw.newLine();
                }
                if (ArrayUtils.contains(Constants.SQL_DATE_TYPES, fied.getSqlType())) {
                    bw.write("\t" + String.format(Constants.BEAN_DATE_FORMAT_EXPRESSION, DateUtils.YYYYMMDD));
                    bw.newLine();
                    //反序列化
                    bw.write("\t" + String.format(Constants.BEAN_DATE_UNFORMAT_EXPRESSION, DateUtils.YYYYMMDD));
                    bw.newLine();
                }
                bw.write("\tprivate  " + fied.getJavaType() + " " + fied.getPropertyName() + ";");
                bw.newLine();
                bw.newLine();
            }
            //get,set方法
            for (FieldInfo fied:tableInfo.getFieldList()){
                String tempField = StringUtils.uperCaseFirstLetter(fied.getPropertyName());//boolean类型需要特殊处理
                bw.write("\tpublic void set"+tempField+"("+fied.getJavaType()+" "+fied.getPropertyName()+") {");
                bw.newLine();
                bw.write("\t\tthis."+fied.getPropertyName()+" = "+fied.getPropertyName()+";");
                bw.newLine();
                bw.write("\t}");
                bw.newLine();
                bw.newLine();
                bw.write("\tpublic "+fied.getJavaType()+" get"+tempField+"() {");
                bw.newLine();
                bw.write("\t\treturn this."+fied.getPropertyName()+";");
                bw.newLine();
                bw.write("\t}");
                bw.newLine();
            }
            StringBuffer toString = new StringBuffer();
            for (FieldInfo fied:tableInfo.getFieldList()){
                String paramName = fied.getPropertyName();
                //判断是否有日期类型
                if (ArrayUtils.contains(Constants.SQL_DATA_TIME_TYPES,fied.getSqlType())){
                    paramName = "DateUtils.format("+paramName+","+"DateTimeEnum.YYYY_MM_DD_HH_MM_SS.getPattern()"+")";
                }else if (ArrayUtils.contains(Constants.SQL_DATE_TYPES,fied.getSqlType())){
                    paramName = "DateUtils.format("+paramName+","+"DateTimeEnum.YYYY_MM_DD.getPattern()"+")";
                }
                toString.append("\""+fied.getComment()+" : \" + ("+paramName+" == null ? \"空\" : "+paramName+") + \",\" + ");
            }
            StringBuffer sb = new StringBuffer();
            sb = sb.append(toString.substring(0,toString.length()-9));

            bw.write("\t@Override");
            bw.newLine();
            bw.write("\tpublic String toString() {");
            bw.newLine();
            bw.write("\t\treturn "+sb+";");
            bw.newLine();
            bw.write("\t}");
            bw.newLine();

            bw.write("}");
            bw.newLine();


        } catch (Exception e) {
            logger.error("创建文件失败", e);
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
    }





}
